package org.example.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.example.mapper.UserMapper;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.utils.SMSUtils;
import org.example.utils.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(Integer userId) {
        return userMapper.findByUserId(userId);
    }

    @Override
    public String sendCode(String phone) throws Exception {
        // 创建 Random 类的实例
        Random random = new Random();

        // 生成一个四位的随机数，范围是1000到9999
        int randomNumber = random.nextInt(9000) + 1000;

        String code = String.valueOf(randomNumber);
        System.out.println("验证码：" + code);
        //发送手机验证码
        SMSUtils.sendMessage(code);

        return code;
    }

    @Override
    public void updatePassword(String username, String phone, String password) {
        userMapper.updatePassword(username, phone, password);
    }

    @Override
    public void updateStatus(Integer status, Integer flag, Integer companyId, Integer userId) {
        userMapper.updateStatus(status, flag, companyId, userId);
    }

    @Override
    public Integer selectMaxId() {
        return userMapper.selectMaxId();
    }

    @Override
    public void updateUser(String phone, Integer userId) {
        userMapper.updateUser(phone, userId);
    }

    @Override
    public void updateHeadImg(String headImg, Integer userId) {
        userMapper.updateHeadImg(headImg, userId);
    }

    @Override
    public void upgradeUser(Integer userId, int status) {
        userMapper.upgradeUser(userId, status);
    }
}
